<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 头部布局 -->
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addPart('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体布局 -->
        <!-- 自定义树形结构的key -->
        <!-- :props="defaultProps" -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addPart(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editPart(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delPart(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 弹层 -->
    <el-dialog v-if="showDialog" :title="isEdit ? '编辑': '添加'" :visible.sync="showDialog">
      <addor-edit :cur-id="curId" :is-edit="isEdit" :all-list="allList" @update-depart="hUpdateDepart" />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
// 第一种数组转结构
import { arrToTree } from '@/utils/index'
// import { tranListToTreeData } from '@/utils/index'
import addorEdit from './AddorEdit'
export default {
  components: {
    addorEdit
  },
  data() {
    return {
      list: [],
      showDialog: false, // 弹层是否显示
      curId: '', // 上级部门id
      isEdit: false, // 标识
      allList: [] // 所有的列表数据
    }
  },
  created() {
    //   页面初始化后调接口获取数据
    this.hGetDepartments()
    // console.log(this.list)
  },
  methods: {
    //   获取列表数据
    async  hGetDepartments() {
      const res = await getDepartments()
      //   console.log(res)
      this.allList = res.data.depts
      //   console.log(this.allList)
      //   调用封装的数组转树状图
      const list = arrToTree(res.data.depts, '')
      //   const list = tranListToTreeData(res.data.depts)
      //   console.log(list)
      this.list = list
    //   console.log(this.list)
    },
    // 点击删除
    async delPart(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        // 删除操作成功
        await delDepartment(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.hGetDepartments()
      }).catch(error => {
        console.log(error)
        this.$message.success('删除操作失败')
      })
    },
    // 点击添加
    async addPart(id) {
      // 点击添加打开弹层
      this.showDialog = true
      // 把父组件id传给子组件
      this.curId = id
      // 定义一个标识  区分是否是编辑页面
      this.isEdit = false
    },
    // 点击子组件的弹层确定 触发该函数
    hUpdateDepart() {
      // 关闭弹层
      this.showDialog = false
      // 更新视图
      this.hGetDepartments()
    },
    // 点击编辑
    editPart(id) {
      // 定义一个标识  区分是否是编辑页面
      this.isEdit = true
      // 点击编辑打开弹层
      this.showDialog = true
      // id传给子组件
      this.curId = id
    }
  }
}
</script>

<style>

</style>
